import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**-邮件产生器属性枚举*/
export const enum E_MailCreator {
	/**产生器id*/
	CREATOR_ID = 0,
	/**邮件来源类型 枚举*/
	MAIL_FROM_TYPE = 1,
	/**邮件配置id*/
	CONFIG_ID = 2,
	/**创建时间*/
	CREATE_TIME = 3,
	/**有效时间，0-无限制*/
	VALID_TIME = 4,
	/**掉落*/
	DROPS = 5,
	/**枚举数量*/
	EnumCount = 6
}


/**-邮件产生器*/
export class MailCreator {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_MAIL_CREATOR_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_MAIL_CREATOR_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: MailCreator[] = [];

	/**创建入口*/
	public static create(json?: string): MailCreator {
		const c = this.POOL.pop() || new MailCreator();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**产生器id (long)*/
	protected _creatorId: number;
	/**邮件来源类型 枚举 (uint8)*/
	protected _mailFromType: number;
	/**邮件配置id (long)*/
	protected _configId: number;
	/**创建时间 (long)*/
	protected _createTime: number;
	/**有效时间，0-无限制 (long)*/
	protected _validTime: number;
	/**掉落*/
	protected _drops: string;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (MailCreator.POOL.indexOf(this) === -1) {
			MailCreator.POOL.push(this);
		}
	}

	/**产生器id*/
	public get creatorId() {
		return this._creatorId;
	}


	/**产生器id*/
	public set creatorId(v: number) {
		this._creatorId = v;
	}


	/**邮件来源类型 枚举*/
	public get mailFromType() {
		return this._mailFromType;
	}


	/**邮件来源类型 枚举*/
	public set mailFromType(v: number) {
		this._mailFromType = v;
	}


	/**邮件配置id*/
	public get configId() {
		return this._configId;
	}


	/**邮件配置id*/
	public set configId(v: number) {
		this._configId = v;
	}


	/**创建时间*/
	public get createTime() {
		return this._createTime;
	}


	/**创建时间*/
	public set createTime(v: number) {
		this._createTime = v;
	}


	/**有效时间，0-无限制*/
	public get validTime() {
		return this._validTime;
	}


	/**有效时间，0-无限制*/
	public set validTime(v: number) {
		this._validTime = v;
	}


	/**掉落*/
	public get drops() {
		return this._drops;
	}


	/**掉落*/
	public set drops(v: string) {
		this._drops = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_MailCreator): any {
		switch (k) {
			case E_MailCreator.CREATOR_ID: return this._creatorId;
			case E_MailCreator.MAIL_FROM_TYPE: return this._mailFromType;
			case E_MailCreator.CONFIG_ID: return this._configId;
			case E_MailCreator.CREATE_TIME: return this._createTime;
			case E_MailCreator.VALID_TIME: return this._validTime;
			case E_MailCreator.DROPS: return this._drops;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_MailCreator, v: any): void {
		switch (k) {
			case E_MailCreator.CREATOR_ID: this._creatorId = v; break;
			case E_MailCreator.MAIL_FROM_TYPE: this._mailFromType = v; break;
			case E_MailCreator.CONFIG_ID: this._configId = v; break;
			case E_MailCreator.CREATE_TIME: this._createTime = v; break;
			case E_MailCreator.VALID_TIME: this._validTime = v; break;
			case E_MailCreator.DROPS: this._drops = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//产生器id
		this._creatorId = 0;
		//邮件来源类型 枚举
		this._mailFromType = 0;
		//邮件配置id
		this._configId = 0;
		//创建时间
		this._createTime = 0;
		//有效时间，0-无限制
		this._validTime = 0;
		//掉落
		this._drops = '';
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._creatorId) {
			json.creatorId = this._creatorId;
		}
		if (this._mailFromType) {
			json.mailFromType = this._mailFromType;
		}
		if (this._configId) {
			json.configId = this._configId;
		}
		if (this._createTime) {
			json.createTime = this._createTime;
		}
		if (this._validTime) {
			json.validTime = this._validTime;
		}
		if (this._drops) {
			json.drops = this._drops;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.creatorId) {
			this._creatorId = json.creatorId;
		}
		if (json.mailFromType) {
			this._mailFromType = json.mailFromType;
		}
		if (json.configId) {
			this._configId = json.configId;
		}
		if (json.createTime) {
			this._createTime = json.createTime;
		}
		if (json.validTime) {
			this._validTime = json.validTime;
		}
		if (json.drops) {
			this._drops = json.drops;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeLongNumber(this._creatorId);
		byte.writeUint8(this._mailFromType);
		byte.writeLongNumber(this._configId);
		byte.writeLongNumber(this._createTime);
		byte.writeLongNumber(this._validTime);
		byte.writeString(this._drops);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._creatorId = byte.readLongNumber();
		this._mailFromType = byte.readUint8();
		this._configId = byte.readLongNumber();
		this._createTime = byte.readLongNumber();
		this._validTime = byte.readLongNumber();
		this._drops = byte.readString();
		return this;
	}


	/**克隆*/
	public clone(): MailCreator {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = MailCreator.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = MailCreator.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//产生器id
			if (idx === E_MailCreator.CREATOR_ID) {
				const oldv = this._creatorId;
				this._creatorId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._creatorId, oldv);
				i += 2;
				continue;
			}
			//邮件来源类型 枚举
			if (idx === E_MailCreator.MAIL_FROM_TYPE) {
				const oldv = this._mailFromType;
				this._mailFromType = us[i + 1];
				IAsync.dispatch(eventID, idx, this._mailFromType, oldv);
				i += 2;
				continue;
			}
			//邮件配置id
			if (idx === E_MailCreator.CONFIG_ID) {
				const oldv = this._configId;
				this._configId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._configId, oldv);
				i += 2;
				continue;
			}
			//创建时间
			if (idx === E_MailCreator.CREATE_TIME) {
				const oldv = this._createTime;
				this._createTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._createTime, oldv);
				i += 2;
				continue;
			}
			//有效时间，0-无限制
			if (idx === E_MailCreator.VALID_TIME) {
				const oldv = this._validTime;
				this._validTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._validTime, oldv);
				i += 2;
				continue;
			}
			//掉落
			if (idx === E_MailCreator.DROPS) {
				const oldv = this._drops;
				this._drops = us[i + 1];
				IAsync.dispatch(eventID, idx, this._drops, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in MailCreator with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: MailCreator): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._creatorId !== other._creatorId) {
			updates.push(E_MailCreator.CREATOR_ID, this._creatorId);
		}
		if (this._mailFromType !== other._mailFromType) {
			updates.push(E_MailCreator.MAIL_FROM_TYPE, this._mailFromType);
		}
		if (this._configId !== other._configId) {
			updates.push(E_MailCreator.CONFIG_ID, this._configId);
		}
		if (this._createTime !== other._createTime) {
			updates.push(E_MailCreator.CREATE_TIME, this._createTime);
		}
		if (this._validTime !== other._validTime) {
			updates.push(E_MailCreator.VALID_TIME, this._validTime);
		}
		if (this._drops !== other._drops) {
			updates.push(E_MailCreator.DROPS, this._drops);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(MailCreator.TOTAL_EVENT_ID, this.getV(0));
	}

}
